package com.woniuxy.interceptor;

import com.woniuxy.pojo.User;
import com.woniuxy.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SessionInterceptor implements HandlerInterceptor{
   /* private Logger logger= LoggerFactory.getLogger(this.getClass());
    @Resource
    private UserService userService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        *//*Subject subject=SecurityUtils.getSubject();
        Cookie[] cookies=request.getCookies();
        String telephone=null;
        String password=null;
        for(Cookie cookie:cookies){
            if(cookie.getName().equals("telephone")){
                telephone=cookie.getValue();
            }
            if(cookie.getName().equals("password")){
                password=cookie.getValue();
            }
        }
        if(subject.isRemembered()){
            logger.debug("从cookie中取出的密码："+password);
            logger.debug("从cookie中取出的账号："+telephone);
            User user =userService.login(telephone,password);
            UsernamePasswordToken token=new UsernamePasswordToken(telephone,password);
            //subject.login(token);
            if(user!=null){
                //获取到的账号密码验证成功 跳转到首页
                Session session=subject.getSession();
                session.setAttribute("USER_SESSION",user);
                logger.debug("进入请求转发前了");
                request.getRequestDispatcher("/index.html").forward(request,response);
                return false;
            }
        }*//*
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
    }*/
}
